<!--
 * @FilePath     : /layui/demo/1034.html
 * @Description  : dropdown.reloadData(id, options)
 * @Date         : 2025-02-20 10:45
 * @Author       : Fang YingGui
 * @LastEditors  : fangyinggui 18715384437@163.com
 * @LastEditTime : 2025-02-20 10:46
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="/layui/dist/css/layui.css" />
  </head>
  <body>
    <div class="layui-form">
      <div class="layui-form-item">
        <label class="layui-form-label">输入时重载</label>
        <div class="layui-input-inline">
          <input
            type="text"
            autocomplete="off"
            placeholder="输入关键字"
            class="layui-input"
            id="ID-dropdown-demo-reloadData"
          />
        </div>
      </div>
    </div>

    <script src="/layui/dist/layui.js"></script>
    <script src="/dropdown/common.js"></script>
    <script>
      layui.use(function () {
        var dropdown = layui.dropdown
        var $ = layui.$
        // 渲染
        var inst = dropdown.render({
          elem: '#ID-dropdown-demo-reloadData',
          data: [
            { id: 1, title: 'Python' },
            { id: 2, title: 'JavaScript' },
            { id: 3, title: 'Java' },
            { id: 4, title: 'C++' },
            { id: 5, title: 'PHP' },
            { id: 6, title: 'Ruby' },
            { id: 7, title: 'Swift' },
            { id: 8, title: 'TypeScript' },
            { id: 9, title: 'Kotlin' },
            { id: 10, title: 'Go' },
          ],
          style: 'min-width: 190px; box-shadow: 1px 1px 11px rgb(0 0 0 / 11%);',
          click: function (data) {
            this.elem.val(data.title)
          },
        })
        // 输入框输入事件
        $(inst.config.elem).on('input propertychange', function () {
          var elem = $(this)
          var value = elem.val().trim()
          // 匹配到对应内容时，重载数据
          var dataNew = inst.config.data.filter(function (item) {
            var exp = new RegExp(value.split('').join('|'), 'i')
            return exp.test(item.title)
          })
          dropdown.reloadData(inst.config.id, {
            data: dataNew, // 匹配到的新数据
            templet: function (d) {
              var exp = new RegExp(value.split('').join('|'), 'gi')
              return d.title.replace(exp, function (str) {
                return '<span style="color: red;">' + str + '</span>'
              })
            },
          })
        })
      })
    </script>
  </body>
</html>
